//
//  MainViewController.m
//  OtakuWorld
//
//  Created by iOS Two on 14-9-19.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "MainViewController.h"
#import "MainTableViewCell.h"
#import "UIImageView+MJWebCache.h"
#import "DetailViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    [MobClick beginLogPageView:@"MainViewController"];
}
-(void)viewWillDisappear:(BOOL)animated {
    [MobClick endLogPageView:@"MainViewController"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getDate];
    if (version >= 7) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"qwe_02"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"qwe_02"] forBarMetrics:UIBarMetricsDefault];
    }
    // Do any additional setup after loading the view.
}

-(void)creatController {
    UIView *ccc = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 64)];
    [self.view addSubview:ccc];
    
    NSMutableArray *viewsArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < 5; i++) {
        UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 165)];
        
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 165)];
        image.backgroundColor = [UIColor whiteColor];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 108, 320, 20)];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = [UIColor whiteColor];
        if (_topImgArray.count != 0) {
            [image setImageURLStr:[_topImgArray objectAtIndex:i] placeholder:[UIImage imageNamed:@"img_newdish"]];
            
            label.text = [_topNameArray objectAtIndex:i];
        }
        [back addSubview:image];
//        [back addSubview:label];
        [viewsArray addObject:back];
    }
    _mainScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 165) animationDuration:3];
    _mainScorllView.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
    _mainScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };
    _mainScorllView.totalPagesCount = ^NSInteger(void){
        return 5;
    };
    _mainScorllView.TapActionBlock = ^(NSInteger pageIndex){
        [MobClick event:@"clickAdvTimes"];
        [self clickAdv:pageIndex];
        NSLog(@"点击了第%d个",pageIndex);
    };
    
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, kHeight - 49) style:UITableViewStylePlain];
    _mainTable.dataSource = self;
    _mainTable.delegate = self;
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _mainTable.tableHeaderView = _mainScorllView;
    [self.view addSubview:_mainTable];
}
-(void)getDate {
    [self showIndeterminateProgress];
    NSURL *url = [NSURL URLWithString:@"http://mrobot.pcgames.com.cn/v3/cms/channels/1"];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:url];
    request.delegate = self;
    request.username = @"info";
    [request startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request {
    SBJsonParser *jsonP = [[SBJsonParser alloc]init];
    if ([request.username isEqualToString:@"info"]) {
        _nameArray = [[NSMutableArray alloc]init];
        _imgArray = [[NSMutableArray alloc]init];
        _dataArray = [[NSMutableArray alloc]init];
        _linkArray = [[NSMutableArray alloc]init];
        _topImgArray = [[NSMutableArray alloc]init];
        _topLinkArray = [[NSMutableArray alloc]init];
        _topNameArray = [[NSMutableArray alloc]init];
        NSArray *array =[[jsonP objectWithData:[request responseData]]objectForKey:@"articleList"];
        for (int i = 0; i < array.count; i++) {
            NSDictionary *dic = [array objectAtIndex:i];
            [_nameArray addObject:[dic objectForKey:@"title"]];
            [_dataArray addObject:[dic objectForKey:@"pubDate"]];
            [_imgArray addObject:[dic objectForKey:@"image"]];
            [_linkArray addObject:[dic objectForKey:@"id"]];
        }
        NSArray *array1 = [[jsonP objectWithData:[request responseData]] objectForKey:@"focus"];
        for (int i = 0; i < array1.count; i++) {
            NSDictionary *dic = [array1 objectAtIndex:i];
            [_topNameArray addObject:[dic objectForKey:@"title"]];
            [_topImgArray addObject:[dic objectForKey:@"image"]];
            [_topLinkArray addObject:[dic objectForKey:@"id"]];
        }
        [self creatController];
        [_mainTable reloadData];
    }
    [HUD hide:YES];
}

-(void)requestFailed:(ASIHTTPRequest *)request {
    NSLog(@"请求失败");
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 67.5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _linkArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[MainTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [cell.image setImageURLStr:[_imgArray objectAtIndex:indexPath.row] placeholder:[UIImage imageNamed:@"1"]];
        
    cell.name.text = [_nameArray objectAtIndex:indexPath.row];

    cell.data.text = [_dataArray objectAtIndex:indexPath.row];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [MobClick event:@"clickInfo"];

    DetailViewController *detail = [[DetailViewController alloc]init];
    detail.url = [_linkArray objectAtIndex:indexPath.row];
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
}

-(void)clickAdv:(NSInteger)index {
    [MobClick event:@"clickInfo"];

    DetailViewController *detail = [[DetailViewController alloc]init];
    detail.url = [_topLinkArray objectAtIndex:index];
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
